package com.yb.guigu.pick.security.generator;

import com.yb.guigu.pick.security.bean.GuiguPickUserRequest;
import com.yb.guigu.pick.security.bean.GuiguPickUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/31 18:35
 * FileName: UsernamePasswordAuthenticationTokenGenerator
 * Description:
 */

@Component
@Primary
@Slf4j
public class DelegatingAuthenticationTokenGenerator implements AuthenticationTokenGenerator{

    private final List<AuthenticationTokenGenerator> generators;

    public DelegatingAuthenticationTokenGenerator(List<AuthenticationTokenGenerator> generators){
            log.info("{}",generators);
            this.generators=generators;
    }
    @Override
    public Authentication unauthenticated(GuiguPickUserRequest userRequest) {

        for (AuthenticationTokenGenerator generator : this.generators) {
            if(generator.support(userRequest)){
                return generator.unauthenticated(userRequest);
            }
        }
        return null;
    }

    @Override
    public boolean support(GuiguPickUserRequest userRequest) {
        return true;
    }

    @Override
    public boolean support(GuiguPickUser guiguPickUser) {
        return true;
    }

    @Override
    public Authentication authenticated(GuiguPickUser guiguPickUser) {

        for (AuthenticationTokenGenerator generator : this.generators) {
            if(generator.support(guiguPickUser)){
                return generator.authenticated(guiguPickUser);
            }
        }
        return null;
    }
}
